package com.atguigu.yygh.shop.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.model.shop.ShopSet;
import com.atguigu.yygh.shop.service.ShopSetService;
import com.atguigu.yygh.vo.shop.ShopSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "店铺设置管理")
@RestController
@RequestMapping("/admin/shop/shopSet")
//@CrossOrigin
public class ShopSetController {

    //注入service
    @Autowired
    private ShopSetService shopSetService;

    // http://localhost:8201/admin/hosp/hospitalSet/findAll

    //1 查询店铺设置表所有信息
    @ApiOperation(value = "获取所有店铺设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
        //调用service的方法
        List<ShopSet> list = shopSetService.list();
        return Result.ok(list);
    }

    //2 逻辑删除店铺设置
    @ApiOperation(value = "逻辑删除店铺设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        boolean flag = shopSetService.removeById(id);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //3 条件查询带分页
    @PostMapping("findPageShopSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) ShopSetQueryVo shopSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<ShopSet> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<ShopSet> wrapper = new QueryWrapper<>();
        String shopname = shopSetQueryVo.getShopname();//商铺名称
        String shopcode = shopSetQueryVo.getShopcode();//商铺编号
        if(!StringUtils.isEmpty(shopname)) {
            wrapper.like("shopname",shopSetQueryVo.getShopname());
        }
        if(!StringUtils.isEmpty(shopcode)) {
            wrapper.eq("shopcode",shopSetQueryVo.getShopcode());
        }

        //调用方法实现分页查询

        IPage<ShopSet> pageHospitalSet = shopSetService.page(page, wrapper);

        //返回结果
        return Result.ok(pageHospitalSet);
    }

    //4 添加店铺设置
    @PostMapping("saveShopSet")
    public Result saveShopSet(@RequestBody ShopSet shopSet) {
        System.out.println("发送请求");
        //设置状态 1 使用 0 不能使用
        shopSet.setStatus(1);
        //签名秘钥
        Random random = new Random();
        shopSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //调用service
        boolean save = shopSetService.save(shopSet);
        if(save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5 根据id获取店铺设置
    @GetMapping("getShopSet/{id}")
    public Result getHospSet(@PathVariable Long id) {
        ShopSet shopSet = shopSetService.getById(id);
        return Result.ok(shopSet);
    }

    //6 修改店铺设置
    @PostMapping("updateShopSet")
    public Result updateShopSet(@RequestBody ShopSet shopSet) {
        boolean flag = shopSetService.updateById(shopSet);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除店铺设置
    @DeleteMapping("batchRemove")
    public Result batchRemoveShopSet(@RequestBody List<Long> idList) {
        shopSetService.removeByIds(idList);
        return Result.ok();
    }

    //8 店铺设置锁定和解锁
    @PutMapping("lockShopSet/{id}/{status}")
    public Result lockShopSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询店铺设置信息
        ShopSet shopSet = shopSetService.getById(id);
        //设置状态
        shopSet.setStatus(status);
        //调用方法
        shopSetService.updateById(shopSet);
        return Result.ok();
    }

    //9 发送签名秘钥
    @PutMapping("sendKey/{id}")
    public Result lockShopSet(@PathVariable Long id) {
        ShopSet shopSet = shopSetService.getById(id);
        String signKey = shopSet.getSignKey();
        String shopcode = shopSet.getShopcode();
        //TODO 发送短信
        return Result.ok();
    }
}
